package com.usian.service.impl;

import com.usian.mapper.GoodMapper;
import com.usian.pojo.Goods;
import com.usian.service.GoodsScanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsScanServiceImpl implements GoodsScanService {

    @Autowired
    private GoodMapper goodMapper;

    @Override
    public void autoScanByMediaNewsId(Integer id) {
        //参数校验
        if (id == null) {
            return;
        }

        List<Goods> goodsList = goodMapper.selectAll();
        Goods goods = goodMapper.selectByPrimaryKey(id);

        if (goods == null) {
            return;
        }

        //判断是不是待审核状态
        if (goods.getShenhe().equals("0")) {

            if (goods.getNum() >= 100) {
                //修改状态为审核失败
                goods.setShenhe("2");
                //set失败原因
                goods.setFail("商品数量大于100");
                goodMapper.updateByPrimaryKey(goods);
                return;
            }

            int i = 0;
            //循环遍历看name是否重复
            for (Goods goodss : goodsList) {
                if (goods.getName().equals(goodss.getName())) {
                    i++;
                }
            }

            if (i >= 2){
                //修改状态为审核失败
                goods.setShenhe("2");
                //set失败原因
                goods.setFail("商品已存在");
                goodMapper.updateByPrimaryKey(goods);
                return;
            }

            //修改状态为审核成功
            goods.setShenhe("1");
            goodMapper.updateByPrimaryKey(goods);

        }

    }
}
